#ifndef PLANE_H
#define PLANE_H

#include <vector3.h>

typedef struct plane
{
  vector3_t point;
  vector3_t normal;
} plane_t;

plane_t *
plane_new( void );

void
plane_free( plane_t *plane );

char *
plane_str( const plane_t plane );


static inline int
plane_intercept( const plane_t plane,
		 const vector3_t base_point, const vector3_t vector,
		 vector3_t *interception_point )
{
  double d = vector3_dot( vector, plane.normal );

  /*
   * if line vector is perpendicular to plane normal
   * line is parallel to plane
   */
  if ( double_is_equal( d, 0.0, DOUBLE_PRECISION ) )
    return 0;

  vector3_t distance = vector3_sub( plane.point, base_point );

  double t = vector3_dot( distance, plane.normal ) / d;

  *interception_point = vector3_add( base_point,
				     vector3_multiply_scalar( vector, t ) );
  return 1;
}

#endif /* PLANE_H */
